package com.example.mycommunity.controller;

import com.example.mycommunity.cache.HotTagCache;
import com.example.mycommunity.dto.HotTagDTO;
import com.example.mycommunity.dto.PageDTO;
import com.example.mycommunity.service.QuestionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class IndexController {



    @Autowired
    private QuestionService questionService;

    @Autowired
    private HotTagCache hotTagCache;

    @GetMapping("/")
    public String index(HttpServletRequest request,
                        Model model,
                        @RequestParam(name = "page",defaultValue = "1")Integer page,
                        @RequestParam(name = "size",defaultValue = "10")Integer size,
                        @RequestParam(name = "search",required = false) String search,
                        @RequestParam(name = "tag",required = false) String tag,
                        @RequestParam(name = "sort",required = false) String sort
                        ){


        PageDTO pageDTO = questionService.list(search,tag,sort,page,size);
        List<String> tags = hotTagCache.getHots();
        model.addAttribute("pageDTO",pageDTO);
        model.addAttribute("search",search);
        model.addAttribute("tags",tags);
        model.addAttribute("tag",tag);
        model.addAttribute("sort",sort);

        return "index";

    }

}
